var app = getApp(),
  $ = require("../../utils/httpHelper.js")
Page({
  data: {
    hasAddress: false,
    addr: '',
    yhqpop: false,
    showdialog: false,
    sel_sku: '',
    good_info: '',
    coupon_list: [],
    good_num: '',
    status: '',
    money: 0,
    z_money: 0,
    fee: 0,
    fee_id: '',
    yhqtext: '使用优惠券',
    ya_money: "",
    liuyan: '',
    pay_cash:false,
    yue:0,
    password:'',
    pay_type:'balance'
  },
  onLoad: function (options) {
    var that = this;
    let params = {
      rd_session: app.globalData.userInfo.rd_session,
      status: '1'
    }
    $.httpPost("cart_list", params, (data, err) => {
      this.setData({
        good_info: data.list[0].list,
        status: 'buy',
        money: data.total_fee,
        z_money: data.total_fee
      })
    })
    $.httpPost("member_shop_coupon", { rd_session: app.globalData.userInfo.rd_session, }, (data, err) => {
      this.setData({
        coupon_list: data.data.list
      })
    })
    let paramadr = {
      rd_session: app.globalData.userInfo.rd_session,
    }
    $.httpPost("address_list", paramadr, (data, err) => {
      if (data.data.lists.length > 0) {
        this.setData({
          addr: data.data.lists[0],
          hasAddress: true
        })
      } else {
        this.setData({
          hasAddress: false
        })
      }
    })
    $.httpPost("recharge_card", { rd_session: app.globalData.userInfo.rd_session }, (data, err) => {
      this.setData({
        yue: data.data.balance + '元'
      })
    })
  },

  tonewAddress() {
    wx.redirectTo({
      url: '/pages/address/address?from=obl2',
    })
  },

  chooseAdr() {
    wx.navigateTo({
      url: '/pages/address/addressli',
    })
  },
  chhoseyhq() {
    this.setData({
      yhqpop: true
    })
  },
  yhqHide() {
    this.setData({
      yhqpop: false
    })
  },
  yhqUse() {
    this.setData({
      yhqtext: "减" + this.data.fee + "元",
      yhqpop: false
    })
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    var arr = e.detail.value.split('-')
    console.log(arr[0] + '===' + this.data.money)
    if (arr[0] < parseInt(this.data.money)) {
      this.setData({
        fee: arr[1],
        fee_id: arr[2],
        z_money: this.data.z_money - arr[1]
      })
    } else {
      wx: wx.showToast({
        title: '不满足满减金额',
        duration: 2000,
      })
    }
    //this.yhqHide()
  },
  formSubmit: function (e) {
    if (!app.check_empty(e.detail.value.username) && !app.check_empty(e.detail.value.userphone)) {
      if (app.checkPhone(e.detail.value.userphone)) {
        var rd_session = wx.getStorageSync('rd_session');
        var param = { rd_session: rd_session, name: e.detail.value.username, phone: e.detail.value.userphone, good_name: e.detail.value.usertoy }
        $.httpPost("edit_member_info", param, (data, err) => {
          wx.showToast({
            title: data.message,
            duration: 2000
          })
          this.setData({
            showdialog: false
          })
        })
      } else {
        wx.showToast({
          title: '手机格式不对哦！~',
          duration: 2000
        })
      }

    } else {
      wx.showToast({
        title: '有信息没有填写哦~',
        duration: 2000
      })
    }
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
  },
  formReset: function () {
    this.setData({
      showdialog: false
    })
    console.log('form发生了reset事件')
  },
  message(e) {
    this.setData({
      liuyan: e.detail.value
    })
  },
  password(e) {
    this.setData({
      password: e.detail.value
    })
  },
  radioChangezf: function (e) {
    if(e.detail.value == 'cash'){
      this.setData({
        pay_cash:true,
        pay_type: 'cash'
      })
    }else{
      this.setData({
        pay_cash: false,
        pay_type: 'balance'
      })
    }
    console.log('radio发生change事件，携带value值为：', e.detail.value)
  },
  topay() {
    if (this.data.pay_cash){
      this.submit()
    }else{
      //余额
      if (app.check_empty(this.data.password)){
        wx.showToast({
          title: '请输入密码',
          duration: 2000
        })
      }else{
        this.submit()
      }
    }
  },

  submit(){
    if (!this.data.addr.province) {
      wx.showToast({
        title: '请先选择地址~',
      })
      return
    }
    let params = {
      message: this.data.liuyan,
      rd_session: app.globalData.userInfo.rd_session,
      order_type: this.data.status,
      total_num: this.data.good_num,
      sku_id: this.data.sku_id,
      coupon_id: this.data.fee_id,
      good_id: this.data.good_id,
      name: this.data.addr.name,
      phone: this.data.addr.phone,
      address: this.data.addr.province + this.data.addr.city + this.data.addr.county + this.data.addr.detail_address,
      pay_type: this.data.pay_type,
      pass:this.data.password
    }
    let result = ''
    let message = ''
    $.httpPost("submit_cart_order", params, (data_order, err) => {
      this.setData({
        order_id: data_order.order_id,
        order_no: data_order.order_no
      })
      result = data_order.result
      message = data_order.message
    })
    let that = this
    app.checkUser((data_user) => {
      if (data_user) {
        if (that.data.status == 'share') {
          app.checkVip((data) => {
            if (data) {
              //支付
              if (that.data.pay_type == 'cash') {
                app.wxPay(that.data.order_no, app.globalData.userInfo.openId, that.data.status);
              } else if (result == 'success') {
                wx.showToast({
                  title: '余额扣款成功',
                  duration: 2000,
                })
                wx.redirectTo({
                  url: '/pages/order/order?id=1',
                })
              } else if (result == 'error'){
                wx.showToast({
                  title: message,
                  duration: 2000,
                })
              }
            } else {
              wx.showToast({
                title: '仅会员租赁',
                duration: 2000
              })
            }
          })
        } else {
          //支付
          if (that.data.pay_type == 'cash') {
            app.wxPay(that.data.order_no, app.globalData.userInfo.openId, that.data.status);
          } else if (result == 'success') {
            wx.showToast({
              title: '余额扣款成功',
              duration: 2000,
            })
            wx.redirectTo({
              url: '/pages/order/order?id=1',
            })
          } else if (result == 'error') {
            wx.showToast({
              title: message,
              duration: 2000,
            })
          }
        }

      } else {
        this.setData({
          showdialog: true
        })
      }
    })
  }


})  